package game.module.sss.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubRoomManager;
import game.module.hall.logic.CheatManager;
import game.module.sss.bean.SssJindouCfg;
import game.module.sss.bean.SssRoom;
import game.module.sss.bean.SssRoomCfg;
import game.module.sss.dao.SssJindouTemplateCache;
import game.module.sss.dao.SssRoomTemplateCache;
import game.module.sss.logic.SssDataManager;
import game.module.sss.logic.SssLogic;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.C2SSSSCreateRoom;
import lua.LuaMessageSss.S2CSSSCreateRoom;

@MsgCodeAnn(msgcode = C2SSSSCreateRoom.id, accessLimit = 500)
public class SssCreateRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssCreateRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SSSSCreateRoom reqMsg = C2SSSSCreateRoom.parse(request);
		int cfg_index = reqMsg.cfg_index;
		byte anti_cheet = reqMsg.anti_cheet;
		byte mapai = reqMsg.mapai;
		byte daqiang = reqMsg.daqiang;
		byte sunzi_a = reqMsg.sunzi_a;
		byte baodao = reqMsg.baodao;
		int jindou = reqMsg.jindouNum;
		byte jindou_type = reqMsg.jindou_type;
		byte roomFeeType = reqMsg.roomFeeType;
		byte is_jindou = reqMsg.is_jindou;
		byte player_count = reqMsg.player_count;
		byte[] add_color = reqMsg.add_color;
		if (add_color == null) {
			add_color = new byte[] {};
		}
		int readlyjindou = 0;
		int playerId = hero.getId();
		logger.info("sss create room,playeyId={},cfg_index={},anti_cheet={},mapai={},daqiang={},sunzi_a={},baodao={},is_jindou={},jindou_type={},player_count={},add_color={}",
				playerId, cfg_index, anti_cheet, mapai, daqiang, sunzi_a, baodao, jindou, jindou_type, player_count, add_color);
		// 参数是否正确
		if ((mapai != 0 && mapai != 1 && mapai != 5 && mapai != 10) || (daqiang != 1 && daqiang != 2) || (sunzi_a != 1 && sunzi_a != 2)
				|| (baodao != 4 && baodao != 5 && baodao != 6) || (jindou_type != 0 && jindou_type != 1 && jindou_type != 2 && jindou_type != 3)
				|| (player_count != 4 && player_count != 5 && player_count != 6 && player_count != 7)) {
			S2ErrorCode respMsg = new S2ErrorCode(-1);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// 加的颜色是否对
		int addColorCount = add_color != null ? add_color.length : 0;
		int fixedAddPlayer = player_count - 4;
		if (fixedAddPlayer != addColorCount && fixedAddPlayer + 1 != addColorCount) {
			S2ErrorCode respMsg = new S2ErrorCode(310);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (add_color != null) {
			for (byte a_color : add_color) {
				if (a_color != 0 && a_color != 1 && a_color != 2 && a_color != 3) {
					S2ErrorCode respMsg = new S2ErrorCode(310);
					hero.writeAndFlush(respMsg.build(hero.alloc()));
					return;
				}
			}
		}
		// 马牌情况下最多6人
		if (mapai != 0 && fixedAddPlayer == 7 && addColorCount == 4) {
			S2ErrorCode respMsg = new S2ErrorCode(311);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// 马牌情况下加色不能加方块
		if (mapai != 0 && add_color != null) {
			for (byte a_color : add_color) {
				if (a_color == 0) {
					S2ErrorCode respMsg = new S2ErrorCode(312);
					hero.writeAndFlush(respMsg.build(hero.alloc()));
					return;
				}
			}
		}
		// 是否在房间中
		boolean isInRoom = SssDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fangka enough
		// int costFangka = 10;
		// if (hero.getPlayerBean().getFang_ka() < costFangka) {
		// S2ErrorCode respMsg = new S2ErrorCode(103);
		// hero.writeAndFlush(respMsg.build(hero.alloc()));
		// return;
		// }
		SssRoomCfg sssRoomCfg = SssRoomTemplateCache.getInstance().getTemplate(cfg_index);
		int costFangka = sssRoomCfg.getCost_fangka();
		if (hero.getPlayerBean().getFang_ka() < costFangka) {
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (is_jindou == 1) {// 金豆房间
			if (hero.getPlayerBean().getClub_id() == 0) {// 没有俱乐部
				S2ErrorCode respMsg = new S2ErrorCode(134);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			SssJindouCfg ratecfg = SssJindouTemplateCache.getInstance().getTemplate(jindou_type);
			if (jindou_type != 3) {// 0 1 2 3 下标
				SssJindouCfg ssscfg = SssJindouTemplateCache.getInstance().getTemplate(jindou_type);
				readlyjindou = ssscfg.getJindou();
				logger.info("sssss={}", ssscfg.getJindou());
			} else {
				readlyjindou = jindou;
			}

			// 判断玩家身上的金豆数量
			if (hero.getPlayerBean().getJin_dou() < readlyjindou * sssRoomCfg.getRound() * ratecfg.getAdmittanceRate()) {
				S2ErrorCode respMsg = new S2ErrorCode(132);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
		}

		SssRoom sssRoom = SssLogic.getInstance().createSssRoom(playerId, sssRoomCfg.getRound());
		sssRoom.setPrevent_cheat(reqMsg.anti_cheet);
		CheatManager.getInstance().openCheatComm(sssRoom.getPrevent_cheat(), sssRoom.getIps(), hero);
		ClubRoomManager.getInstance().openClubRoom(hero, sssRoom);
		sssRoom.setMapai(mapai);
		sssRoom.setIs_jindou(is_jindou);
		sssRoom.setDaqiang(daqiang);
		sssRoom.setSunzi_a(sunzi_a);
		sssRoom.setBaodao(baodao);
		// ddzRoom.setIs_jindou(is_jindou);
		sssRoom.setJindou_type(jindou_type);
		sssRoom.setRoomFee(costFangka);
		sssRoom.setJindou(readlyjindou);
		sssRoom.setRoomFeeType(roomFeeType);
		sssRoom.setPlayer_count(player_count);
		sssRoom.setAdd_color(add_color);
		S2CSSSCreateRoom respMsg = new S2CSSSCreateRoom();
		respMsg.room_id = sssRoom.getUuid();
		respMsg.round_count = sssRoom.getRoundCount();
		respMsg.anti_cheet = anti_cheet;
		respMsg.mapai = mapai;
		respMsg.daqiang = daqiang;
		respMsg.sunzi_a = sunzi_a;
		respMsg.baodao = baodao;
		respMsg.jindouNum = sssRoom.getJindou();
		respMsg.is_jindou = is_jindou;
		// respMsg.jindou_type = jindou_type;
		respMsg.roomFeeType = reqMsg.roomFeeType;
		respMsg.player_count = player_count;
		respMsg.add_color = add_color;
		// ret
		logger.info("sss created jindouNum={}", respMsg.jindouNum);
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
